package com.controller;

import com.Service.AdminMenu;
import com.Service.MenuService;
import com.ServiceImpl.MenuServiceImpl;
import com.pojo.Car;
import com.pojo.Record;
import com.pojo.User;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;
public class AdminMenuImpl implements AdminMenu {
    private User user;
    private Scanner sc;
    private MenuService menuService;

    public AdminMenuImpl(User user) {
        this.user = user;
        sc = new Scanner(System.in);
        menuService = new MenuServiceImpl();
}

    @Override
    public void mainMenu() {
        List<Car> allCar = menuService.getAllCar(user);
        System.out.println("编号\t汽车名称\t备注\t品牌\t类型\t价格\t是否可租");
        allCar.forEach(car -> System.out.println(car));
        System.out.println("输入0退出");
        System.out.println("输入1查看所有汽车");
        System.out.println("输入2+汽车编号查看指定汽车");
        System.out.println("输入3添加汽车");
        System.out.println("输入4+汽车编号修改汽车信息");
        System.out.println("输入5查看所有用户全部租赁信息");
        System.out.println("输入6+用户编号查看指定用户的租赁信息");
        System.out.println("输入7+汽车编号查看指定车辆的租赁信息");
        System.out.println("输入8查看所有用户信息");
        String choice = sc.next();
        if (choice.length() > 2) {
            String[] str = choice.split("\\+");
            switch (str[0]) {
                case "2":
                    showCarById(str[1]);
                    break;
                case "4":
                    updateCarById(str[1]);
                    break;
                case "6":
                    showRecordByUserId(str[1]);
                    break;
                case "7":
                    showRecordByCarId(str[1]);
                    break;
                default:
                    System.out.println("输入错误");
                    mainMenu();
                    return;

            }
        } else {
            switch (choice) {
                case "0":
                    new CommonMenuImpl().choice();
                    return;
                case "1":
                    showAllCar();
                    break;
                case "3":
                    addCar();
                    break;
                case "5":
                    showAllRecord();
                    break;
                case "8":
                    showAllUser();
                    break;
                default:
                    System.out.println("输入错误");
                    mainMenu();
                    return;
            }
        }
    }

    @Override
    public void showAllCar() {
        List<Car> allCar = menuService.getAllCar(user);
        if (allCar == null) {
            System.out.println("没有车辆信息");
        } else {
            System.out.println("编号\t汽车名称\t备注\t品牌\t类型\t价格\t是否可租");
            allCar.forEach(car -> System.out.println(car));
        }
        back();

    }

    @Override
    public void showCarById(String id) {
        Car car = menuService.getCarByid(user, id);
        if (car == null) {
            System.out.println("没有车辆信息");
        } else {
            System.out.println("编号\t汽车名称\t备注\t品牌\t类型\t价格\t是否可租");
            System.out.println(car);
        }
        back();
    }

    @Override
    public void addCar() {
        System.out.println("=============================");
        System.out.println("请分别输入以下信息");
        System.out.println("-----------------------------");

        System.out.print("请输入品牌：");
        String brand = sc.next();
        System.out.println("----------------------");
        System.out.print("请输入类型：");
        String type = sc.next();
        System.out.println("--------------------------");
        System.out.print("型号：");
        String name = sc.next();
        System.out.println("--------------------------");
        System.out.print("车牌号：");
        String carNumber = sc.next();
        System.out.println("--------------------------");
        System.out.print("概要：");
        String remark = sc.next();
        System.out.println("--------------------------");
        System.out.print("颜色：");
        String color = sc.next();
        System.out.println("--------------------------");
        System.out.print("车辆价值：");
        int carPrice = sc.nextInt();
        System.out.println("--------------------------");
        System.out.print("每日租金：");
        BigDecimal price = sc.nextBigDecimal();
        System.out.println("--------------------------");
        System.out.println("是否可借(0:可借 1:不可借)");
        int rentInt = sc.nextInt();
        boolean rent;
        if (rentInt == 0) {
            rent = true;
        } else if (rentInt == 1) {
            rent = false;
        } else {
            System.out.println("输入错误");
            addCar();
            return;
        }
        System.out.println("--------------------------");
        System.out.println("是否上架(0:上架 1:下架)");
        int statusInt = sc.nextInt();
        boolean status;
        if (statusInt == 0) {
            status = true;
        } else if (statusInt == 1) {
            status = false;
        } else {
            System.out.println("输入错误");
            addCar();
            return;
        }
        List<Car> cars = menuService.getAllCar(user);
        Car car = new Car("下面定义", name, remark, brand, type, price, rent, status, carNumber, carPrice, color);
        if (cars.size() == 0) {
            car.setId("101");
        } else {
            car.setId((Integer.parseInt(cars.get(cars.size() - 1).getId()) + 1) + "");
        }
        boolean a = menuService.addCar(car);
        System.out.println("添加成功");
        back();
    }

    @Override
    public void updateCarById(String id) {
        System.out.println("请输入需要修改的内容的编号：");
        System.out.println("1.租赁价格 2.上架下架");
        //或许需要修改的选项
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                System.out.println("请输入新的租赁价格：");
                BigDecimal price = sc.nextBigDecimal();
                boolean resultPrice = menuService.updateCarById(id, "price", price);
                if (resultPrice) {
                    System.out.println("修改成功");
                } else {
                    System.out.println("修改失败");
                }
                break;
            case 2:
                System.out.println("请输入是否上架1.上架 2.下架");
                boolean status;
                int choiceInt = sc.nextInt();
                BigDecimal choiceDecimal = new BigDecimal(choiceInt);
                if (choice == 1 || choice == 2) {
                    boolean resultStatus = menuService.updateCarById(id, "status", choiceDecimal);
                    if (resultStatus) {
                        System.out.println("修改成功");
                    } else {
                        System.out.println("修改失败");
                    }
                } else {
                    System.out.println("输入错误");
                    return;
                }
                break;
            default:
                System.out.println("输入错误");
                break;
        }
        back();
    }

    @Override
    public void showAllRecord() {
        List<Record> allRecords = menuService.getAllRecords();
        if (allRecords.size() == 0) {
            System.out.println("没有借车");
        } else {
            System.out.println("订单编号\t用户编号\t车辆编号\t借车时间\t还车时间\t金额");
            allRecords.stream().forEach(record -> System.out.println(record));
        }
        back();
    }

    @Override
    public void showRecordByUserId(String id) {
        User user = menuService.getUserById(id);
        List<Record> recordsByUser = menuService.getRecordsByUser(user);
        if (recordsByUser.size() == 0) {
            System.out.println("没有订单信息");
        } else {
            System.out.println("订单编号\t用户编号\t车辆编号\t借车时间\t还车时间\t金额");
            recordsByUser.stream().forEach(record -> System.out.println(record));
        }
        back();
    }

    @Override
    public void showRecordByCarId(String id) {
        Car car = menuService.getCarByid(user,id);
        List<Record> recordsByCar = menuService.getRecordsByCar(car);
        if (recordsByCar.size() == 0) {
            System.out.println("没有借车信息");
        } else {
            System.out.println("订单编号\t用户编号\t车辆编号\t借车时间\t还车时间\t金额");
            recordsByCar.stream().forEach(record -> System.out.println(record));
        }
        back();
    }

    @Override
    public void showAllUser() {
        List<User> allUser = menuService.getAllUser();
        System.out.println("编号 用户名\t密码");
        allUser.forEach(user1 -> System.out.println(user1));
        back();
    }

    public void back() {
        System.out.println("输入任意字符返回上一级");
        sc.next();
        mainMenu();
    }
}
